/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package app;
import static app.Stats.message;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Damian
 */
public class Name extends javax.swing.JFrame {
   static String name;
   static String pass;
   static String adminPassword = "admin";
   static String adminLogin = "admin";

    private static void dispone() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
   private int _errorPass = 0;
    /**
     * Creates new form Name
     */
    
    
    
    
    
    public Name() {
        initComponents();
        jTextField2.setEditable(false); 
        setTitle("Okno logowania do programu SleepAlert");
        setLocationRelativeTo(null);
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Zatwierdź");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jTextField1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTextField1CaretUpdate(evt);
            }
        });

        jLabel1.setText("Identyfikator użytkownika:");

        jLabel2.setText("Hasło logowania administratora:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("LOGOWANIE:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(296, 296, 296)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                            .addComponent(jTextField2)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
       if (jTextField1.getText().length() == 0) {
           JOptionPane.showMessageDialog(this, "Musisz podać identyfikator, aby uruchomić program.");
       
           return;
       }
       if (jTextField1.getText().equals(adminLogin) && jTextField2.getText().equals("")) {
           JOptionPane.showMessageDialog(this, "Jeżeli jesteś administratorem podaj hasło.");
           return;
       }
       if (jTextField1.getText().equals(adminLogin) && !jTextField2.getText().equals(adminPassword)) {
           JOptionPane.showMessageDialog(this, "Hasło nie prawidłowe, czy napewno jesteś administratorem?");
           _errorPass++;
           if (_errorPass == 3){
               Stats.currentDate();
               Stats.currentTime();
               Stats.message ="PRÓBA WŁAMANIA NA KONTO ADMINISTRATORA! o godzinie: "+Stats._currentTime;
               
                    BufferedWriter zapis = null;
               try {
                   zapis = new BufferedWriter(new FileWriter("Statystyki\\"+Stats._currentDate+".txt",true));
               } catch (IOException ex) {
                   Logger.getLogger(Name.class.getName()).log(Level.SEVERE, null, ex);
               }
            String sep = System.getProperty("line.separator");
               try {
                   zapis.write(sep+Stats.message);
               } catch (IOException ex) {
                   Logger.getLogger(Name.class.getName()).log(Level.SEVERE, null, ex);
               }
               try {
                   zapis.close();
               } catch (IOException ex) {
                   Logger.getLogger(Name.class.getName()).log(Level.SEVERE, null, ex);
               }

               dispose();
                
           }
           return;
       }
       Name.name = jTextField1.getText();
       Name.pass = jTextField2.getText();
       //setVisible(false);
        
       SleepAlert alerter = new SleepAlert();
       alerter.start();
       dispose();
       
    }//GEN-LAST:event_jButton1MouseClicked

    private void jTextField1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextField1CaretUpdate
        if(jTextField1.getText().equals(adminLogin)){
            jTextField2.setEditable(true);
        }
        else{        
            jTextField2.setEditable(false);  
        }        
    }//GEN-LAST:event_jTextField1CaretUpdate

 

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
